package legumes.client.ui.widgets.utils;

import legumes.shared.domain.Price;
import legumes.shared.domain.Quantity;

import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.NumberFormat;

public class Formatters {

	private static NumberFormat quantityNumberFormat = NumberFormat.getFormat("0.##");

	public static String formatQuantity(float quantity_) {
		String quantityStr = "";
		if (quantity_ != 1f) {
			quantityStr = quantityNumberFormat.format( quantity_ );
		}
		return quantityStr;
	}
	
	public static String formatQuantity(Object qte_) {
		float fQte = 0.0f;
		try{
			if( qte_ instanceof Quantity){
				fQte = ((Quantity)qte_).getValue();
			} else {
				fQte = extractFloat(qte_);
			}
		} catch(Exception e_){
			GWT.log("Error while formatting amount : formatQuantity Object=" + fQte, e_);
			fQte = 0.0f;
		}
		
		return formatQuantity( fQte );
	}
	
	private static String EURO_CURRENCY = "&#8364;";
	transient private static NumberFormat priceNumberFormat = NumberFormat.getFormat("0.00");
	
	public static String getCurrentCurrencyAsHtml(){
		return EURO_CURRENCY;
	}
	
	public static String formatPrice(float amount_) {
		return formatPrice( amount_, false );
	}
	
	public static String formatPrice(float amount_, boolean withCurency_) {
		String currency = "";
		if(withCurency_){
			currency = "&nbsp;" + getCurrentCurrencyAsHtml();
		}
		return priceNumberFormat.format( amount_ ) + currency;
	}
	
	public static String formatPrice(Object amount_){
		return formatPrice(amount_, false);
	}
	
	public static String formatPrice(Object amount_, boolean withCurency_) {
		float fAmount = 0.0f;
		try{
			if( amount_ instanceof Price){
				fAmount = ((Price)amount_).getAmount();
			} else {
				fAmount = extractFloat(amount_);
			}
		} catch(Exception e_){
			GWT.log("Error while formatting amount : formatPrice Object=" + amount_, e_);
			fAmount = 0.0f;
		}
		
		return formatPrice( fAmount, withCurency_ );
	}
	
	
	public static float extractFloat(Object obj_) throws Exception {
		float fObj = 0.0f;
		if( obj_ != null){
			if( obj_ instanceof String){
				fObj = Float.parseFloat( ((String) obj_).replace(',', '.') );
			} else if(obj_ instanceof Float ){
				fObj = (Float) obj_;
	    	} else if(obj_ instanceof Integer ){
				fObj = (Integer) obj_;
	    	} else if(obj_ instanceof Number) {
				fObj = ((Number)obj_).floatValue();
			} else {
				fObj = Float.parseFloat( obj_.toString().replace(',', '.') );
			}
		}
		return fObj;
	}
	
	public static int extractInteger(Object obj_) throws Exception {
		int iObj = 0;
		if( obj_ != null){
			if( obj_ instanceof String){
				iObj = Integer.parseInt( ((String) obj_) );
			} else if(obj_ instanceof Float ){
				iObj = (Integer) obj_;
	    	} else if(obj_ instanceof Integer ){
				iObj = (Integer) obj_;
	    	} else if(obj_ instanceof Number) {
				iObj = ((Number)obj_).intValue();
			} else {
				iObj = Integer.parseInt( obj_.toString() );
			}
		}
		return iObj;
	}
}
